class PriceListsController < ApplicationController
  before_filter :require_admin,       :only => [:index, :new,   :create,  :edit,    :update,  :destroy]
  before_filter :find_price_list,     :only => [:show,  :edit,  :update,  :destroy]

  def index
    @price_lists = PriceList.find(:all)
  end

  def show
  end

  def new
    @price_list = PriceList.new
  end

  def create
    @price_list = PriceList.new(params[:price_list])
    @price_list.save!
    flash[:update] = 'The price list has been added'
    redirect_to settings_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'There was a problem adding the price list. See below.'
    redirect_to settings_url
  end

  def edit
  end

  def update
    @price_list.update_attributes!(params[:price_list])
    flash[:update] = 'The information for this price list has been updated.'
    redirect_to settings_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    redirect_to settings_url
  end

  def destroy
    @price_list.destroy
    flash[:update] = 'The price list has been removed.'
    redirect_to settings_url
  end

protected
  def require_admin
    if logged_in?
      if !current_user.is_admin?
        flash[:error] = "Must be an administrator to do that, sorry."
        redirect_back_or_default('/')
      end
    else
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def find_price_list
    @price_list = PriceList.find params[:id]
  end
end
